<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 颜色规格模型
 * @author huajie <banhuajie@163.com>
 */

class ColorSpecModel extends Model {

    /* 自动验证规则 */
    protected $_validate = array(
        array('color_name', 'require', '字段名必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('color_name', 'checkName', '颜色名称已存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
        array('color_code', 'require', '字段定义必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('color_num', '1,5', '注释长度不能超过5个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
        array('color_num', 'checkNum', '颜色代码已存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
    );

    /* 操作的表名 */
    protected $table_name = null;

    /**
     * 新增或更新一个颜色规格
     * @return boolean fasle 失败 ， int  成功 返回完整的数据
     * @author huajie <banhuajie@163.com>
     */
    public function update($data = null){
        /* 获取数据对象 */
        //$data = I('post.list');
        $data = $this->create($data);
        if(empty($data)){
            return false;
        }
        /* 添加或新增属性 */
        if(empty($data['id'])){ //新增属性
            $id = $this->add();
            if(!$id){
                $this->error = '新增属性出错！';
                return false;
            }
        } else { //更新数据
            $status = $this->save();
            if(false === $status){
                $this->error = '更新属性出错！';
                return false;
            }
        }
        return $data;
    }

    /**
     * 检查同一张表是否有相同的字段
     * @author huajie <banhuajie@163.com>
     */
    protected function checkName( ){
        $data = I('post.list');
        if(!empty($data))
        {
            $data = $data[0];
        }else
        {
            $data = I('post.');
        }
        $name = $data['color_name'];
        $id = $data['id'];
        $map = array('color_name'=>$name);
        if(!empty($id)){
            $map['id'] = array('neq', $id);
        }
        $res = $this->where($map)->find();
        return empty($res);
    }

    protected function checkNum( ){
        $data = I('post.list');
        if(!empty($data))
        {
            $data = $data[0];
        }else
        {
            $data = I('post.');
        }
        $num = $data['color_num'];
        $id = $data['id'];
        $map = array('color_num'=>$num);
        if(!empty($id)){
            $map['id'] = array('neq', $id);
        }
        $res = $this->where($map)->find();
        return empty($res);
    }

}
